{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\xkc\\Desktop\\benchmark_xkc\n"
     ]
    }
   ],
   "source": [
    "cd C:\\Users\\xkc\\Desktop\\benchmark_xkc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\xkc\\anaconda3\\envs\\py38\\lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:975: FutureWarning: `sparse` was renamed to `sparse_output` in version 1.2 and will be removed in 1.4. `sparse_output` is ignored unless you leave `sparse` to its default value.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import torch\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "from torchvision import transforms\n",
    "from PIL import Image\n",
    "from sklearn.preprocessing import LabelEncoder, OneHotEncoder\n",
    "\n",
    "class Derm7PtDataset(Dataset):\n",
    "    \"\"\" PyTorch Dataset for Derm7Pt \"\"\"\n",
    "\n",
    "    def __init__(self, df, transform=None):\n",
    "        \"\"\"\n",
    "        初始化数据集\n",
    "        :param df: 预处理后的 Pandas DataFrame\n",
    "        :param transform: 图像预处理\n",
    "        \"\"\"\n",
    "        self.df = df\n",
    "        self.transform = transform\n",
    "\n",
    "    def __len__(self):\n",
    "        \"\"\" 返回数据集大小 \"\"\"\n",
    "        return len(self.df)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        \"\"\" 取单个样本 \"\"\"\n",
    "        row = self.df.iloc[idx]\n",
    "\n",
    "        # 加载临床图像\n",
    "        clinic_img = Image.open(row[\"clinic_path\"]).convert(\"RGB\")\n",
    "        derm_img = Image.open(row[\"derm_path\"]).convert(\"RGB\")\n",
    "\n",
    "        # 进行变换\n",
    "        if self.transform:\n",
    "            clinic_img = self.transform(clinic_img)\n",
    "            derm_img = self.transform(derm_img)\n",
    "\n",
    "        # 加载数值特征\n",
    "        metadata = torch.tensor(row[\"metadata\"], dtype=torch.float32)\n",
    "\n",
    "        # 目标类别（分类标签）\n",
    "        label = torch.tensor(row[\"diagnosis_encoded\"], dtype=torch.long)\n",
    "\n",
    "        return clinic_img, derm_img, metadata, label\n",
    "\n",
    "data_dir = r\"C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7pt\"\n",
    "batch_size=32\n",
    "\"\"\"\n",
    "预处理 Derm7Pt 数据集并返回 PyTorch DataLoader\n",
    ":param data_dir: 数据目录\n",
    ":param batch_size: batch size\n",
    ":return: train_loader, valid_loader, test_loader\n",
    "\"\"\"\n",
    "# 加载 meta 数据\n",
    "meta_path = os.path.join(data_dir, \"meta/meta.csv\")\n",
    "df = pd.read_csv(meta_path)\n",
    "\n",
    "# 处理图像路径\n",
    "df[\"clinic_path\"] = df[\"clinic\"].apply(lambda x: os.path.join(data_dir, \"images\", x))\n",
    "df[\"derm_path\"] = df[\"derm\"].apply(lambda x: os.path.join(data_dir, \"images\", x))\n",
    "\n",
    "# 处理分类标签\n",
    "label_encoder = LabelEncoder()\n",
    "df[\"diagnosis_encoded\"] = label_encoder.fit_transform(df[\"diagnosis\"])\n",
    "\n",
    "# 处理 7 点检查法评分（标准化）\n",
    "seven_point_cols = [\n",
    "    \"pigment_network\", \"streaks\", \"pigmentation\", \"regression_structures\",\n",
    "    \"dots_and_globules\", \"blue_whitish_veil\", \"vascular_structures\"\n",
    "]\n",
    "for col in seven_point_cols:\n",
    "    df[col] = df[col].map({\"absent\": 0, \"present\": 1}).fillna(0)\n",
    "\n",
    "# 处理元数据（性别、位置、病变形态）\n",
    "categorical_cols = [\"sex\", \"location\", \"elevation\", \"management\"]\n",
    "onehot_encoder = OneHotEncoder(sparse=False, handle_unknown=\"ignore\")\n",
    "encoded_metadata = onehot_encoder.fit_transform(df[categorical_cols])\n",
    "\n",
    "# 组合元数据\n",
    "df[\"metadata\"] = list(encoded_metadata)\n",
    "\n",
    "# 读取数据集划分索引\n",
    "train_idx = pd.read_csv(os.path.join(data_dir, \"meta/train_indexes.csv\"))[\"indexes\"].tolist()\n",
    "valid_idx = pd.read_csv(os.path.join(data_dir, \"meta/valid_indexes.csv\"))[\"indexes\"].tolist()\n",
    "test_idx = pd.read_csv(os.path.join(data_dir, \"meta/test_indexes.csv\"))[\"indexes\"].tolist()\n",
    "\n",
    "# 划分数据集\n",
    "df_train = df.iloc[train_idx].reset_index(drop=True)\n",
    "df_valid = df.iloc[valid_idx].reset_index(drop=True)\n",
    "df_test = df.iloc[test_idx].reset_index(drop=True)\n",
    "\n",
    "# 图像转换\n",
    "image_transform = transforms.Compose([\n",
    "    transforms.Resize((224, 224)),\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n",
    "])\n",
    "\n",
    "# 创建 Dataset\n",
    "train_dataset = Derm7PtDataset(df_train, transform=image_transform)\n",
    "valid_dataset = Derm7PtDataset(df_valid, transform=image_transform)\n",
    "test_dataset = Derm7PtDataset(df_test, transform=image_transform)\n",
    "\n",
    "# 创建 DataLoader\n",
    "train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)\n",
    "valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, num_workers=4)\n",
    "test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['sex', 'location', 'elevation', 'management']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categorical_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 1., ..., 0., 1., 0.],\n",
       "       [1., 0., 0., ..., 0., 1., 0.],\n",
       "       [1., 0., 0., ..., 0., 1., 0.],\n",
       "       ...,\n",
       "       [0., 1., 0., ..., 1., 0., 0.],\n",
       "       [0., 1., 0., ..., 0., 0., 1.],\n",
       "       [1., 0., 0., ..., 0., 1., 0.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "encoded_metadata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...\n",
       "1       [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...\n",
       "2       [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...\n",
       "3       [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...\n",
       "4       [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...\n",
       "                              ...                        \n",
       "1006    [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...\n",
       "1007    [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...\n",
       "1008    [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...\n",
       "1009    [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...\n",
       "1010    [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...\n",
       "Name: metadata, Length: 1011, dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"metadata\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train_dataset[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(413, 23)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_dataset.df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>case_num</th>\n",
       "      <th>diagnosis</th>\n",
       "      <th>seven_point_score</th>\n",
       "      <th>pigment_network</th>\n",
       "      <th>streaks</th>\n",
       "      <th>pigmentation</th>\n",
       "      <th>regression_structures</th>\n",
       "      <th>dots_and_globules</th>\n",
       "      <th>blue_whitish_veil</th>\n",
       "      <th>vascular_structures</th>\n",
       "      <th>...</th>\n",
       "      <th>sex</th>\n",
       "      <th>management</th>\n",
       "      <th>clinic</th>\n",
       "      <th>derm</th>\n",
       "      <th>case_id</th>\n",
       "      <th>notes</th>\n",
       "      <th>clinic_path</th>\n",
       "      <th>derm_path</th>\n",
       "      <th>diagnosis_encoded</th>\n",
       "      <th>metadata</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>17</td>\n",
       "      <td>basal cell carcinoma</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>male</td>\n",
       "      <td>excision</td>\n",
       "      <td>NFL/Nfl067.jpg</td>\n",
       "      <td>NFL/Nfl068.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>0</td>\n",
       "      <td>[0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>basal cell carcinoma</td>\n",
       "      <td>4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>male</td>\n",
       "      <td>excision</td>\n",
       "      <td>NGL/NGL046.JPG</td>\n",
       "      <td>NGL/Ngl047.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>0</td>\n",
       "      <td>[0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>basal cell carcinoma</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>female</td>\n",
       "      <td>excision</td>\n",
       "      <td>NML/Nml105.jpg</td>\n",
       "      <td>NML/Nml106.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>0</td>\n",
       "      <td>[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>28</td>\n",
       "      <td>basal cell carcinoma</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>male</td>\n",
       "      <td>excision</td>\n",
       "      <td>Ael/Ael505bis.jpg</td>\n",
       "      <td>Ael/Ael505.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>0</td>\n",
       "      <td>[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>30</td>\n",
       "      <td>basal cell carcinoma</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>female</td>\n",
       "      <td>excision</td>\n",
       "      <td>Gzl/gzl05.jpg</td>\n",
       "      <td>Gzl/gzl06.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>0</td>\n",
       "      <td>[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>34</td>\n",
       "      <td>basal cell carcinoma</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>female</td>\n",
       "      <td>excision</td>\n",
       "      <td>FCL/Fcl061.jpg</td>\n",
       "      <td>FCL/Fcl062.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>0</td>\n",
       "      <td>[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>44</td>\n",
       "      <td>blue nevus</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>male</td>\n",
       "      <td>no further examination</td>\n",
       "      <td>NFL/Nfl045.jpg</td>\n",
       "      <td>NFL/Nfl046.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>1</td>\n",
       "      <td>[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>51</td>\n",
       "      <td>blue nevus</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>female</td>\n",
       "      <td>no further examination</td>\n",
       "      <td>NML/Nml088.jpg</td>\n",
       "      <td>NML/Nml089.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>1</td>\n",
       "      <td>[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>61</td>\n",
       "      <td>blue nevus</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>female</td>\n",
       "      <td>clinical follow up</td>\n",
       "      <td>Gzl/gzl87.jpg</td>\n",
       "      <td>Gzl/gzl88.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>1</td>\n",
       "      <td>[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>63</td>\n",
       "      <td>blue nevus</td>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>female</td>\n",
       "      <td>clinical follow up</td>\n",
       "      <td>FAL/Fal010.jpg</td>\n",
       "      <td>FAL/Fal012.jpg</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...</td>\n",
       "      <td>1</td>\n",
       "      <td>[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   case_num             diagnosis  seven_point_score  pigment_network  \\\n",
       "0        17  basal cell carcinoma                  3              0.0   \n",
       "1        18  basal cell carcinoma                  4              0.0   \n",
       "2        23  basal cell carcinoma                  3              0.0   \n",
       "3        28  basal cell carcinoma                  0              0.0   \n",
       "4        30  basal cell carcinoma                  0              0.0   \n",
       "5        34  basal cell carcinoma                  3              0.0   \n",
       "6        44            blue nevus                  0              0.0   \n",
       "7        51            blue nevus                  0              0.0   \n",
       "8        61            blue nevus                  2              0.0   \n",
       "9        63            blue nevus                  2              0.0   \n",
       "\n",
       "   streaks  pigmentation  regression_structures  dots_and_globules  \\\n",
       "0      0.0           0.0                    0.0                0.0   \n",
       "1      0.0           0.0                    0.0                0.0   \n",
       "2      0.0           0.0                    0.0                0.0   \n",
       "3      0.0           0.0                    0.0                0.0   \n",
       "4      0.0           0.0                    0.0                0.0   \n",
       "5      0.0           0.0                    0.0                0.0   \n",
       "6      0.0           0.0                    0.0                0.0   \n",
       "7      0.0           0.0                    0.0                0.0   \n",
       "8      0.0           0.0                    0.0                0.0   \n",
       "9      0.0           0.0                    0.0                0.0   \n",
       "\n",
       "   blue_whitish_veil  vascular_structures  ...     sex  \\\n",
       "0                  1                  0.0  ...    male   \n",
       "1                  0                  0.0  ...    male   \n",
       "2                  1                  0.0  ...  female   \n",
       "3                  0                  0.0  ...    male   \n",
       "4                  0                  0.0  ...  female   \n",
       "5                  0                  0.0  ...  female   \n",
       "6                  0                  0.0  ...    male   \n",
       "7                  0                  0.0  ...  female   \n",
       "8                  1                  0.0  ...  female   \n",
       "9                  1                  0.0  ...  female   \n",
       "\n",
       "               management             clinic            derm case_id notes  \\\n",
       "0                excision     NFL/Nfl067.jpg  NFL/Nfl068.jpg     NaN   NaN   \n",
       "1                excision     NGL/NGL046.JPG  NGL/Ngl047.jpg     NaN   NaN   \n",
       "2                excision     NML/Nml105.jpg  NML/Nml106.jpg     NaN   NaN   \n",
       "3                excision  Ael/Ael505bis.jpg  Ael/Ael505.jpg     NaN   NaN   \n",
       "4                excision      Gzl/gzl05.jpg   Gzl/gzl06.jpg     NaN   NaN   \n",
       "5                excision     FCL/Fcl061.jpg  FCL/Fcl062.jpg     NaN   NaN   \n",
       "6  no further examination     NFL/Nfl045.jpg  NFL/Nfl046.jpg     NaN   NaN   \n",
       "7  no further examination     NML/Nml088.jpg  NML/Nml089.jpg     NaN   NaN   \n",
       "8      clinical follow up      Gzl/gzl87.jpg   Gzl/gzl88.jpg     NaN   NaN   \n",
       "9      clinical follow up     FAL/Fal010.jpg  FAL/Fal012.jpg     NaN   NaN   \n",
       "\n",
       "                                         clinic_path  \\\n",
       "0  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "1  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "2  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "3  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "4  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "5  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "6  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "7  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "8  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "9  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...   \n",
       "\n",
       "                                           derm_path diagnosis_encoded  \\\n",
       "0  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 0   \n",
       "1  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 0   \n",
       "2  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 0   \n",
       "3  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 0   \n",
       "4  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 0   \n",
       "5  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 0   \n",
       "6  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 1   \n",
       "7  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 1   \n",
       "8  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 1   \n",
       "9  C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7p...                 1   \n",
       "\n",
       "                                            metadata  \n",
       "0  [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...  \n",
       "1  [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...  \n",
       "2  [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...  \n",
       "3  [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...  \n",
       "4  [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...  \n",
       "5  [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...  \n",
       "6  [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, ...  \n",
       "7  [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...  \n",
       "8  [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...  \n",
       "9  [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, ...  \n",
       "\n",
       "[10 rows x 23 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_dataset.df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch = next(iter(train_loader))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\xkc\\anaconda3\\envs\\py38\\lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:975: FutureWarning: `sparse` was renamed to `sparse_output` in version 1.2 and will be removed in 1.4. `sparse_output` is ignored unless you leave `sparse` to its default value.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(<torch.utils.data.dataloader.DataLoader at 0x1f5b19f3b50>,\n",
       " <torch.utils.data.dataloader.DataLoader at 0x1f5b19f35b0>,\n",
       " <torch.utils.data.dataloader.DataLoader at 0x1f5b19f3700>)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 运行预处理\n",
    "data_dir = r\"C:\\Users\\xkc\\Desktop\\benchmark_xkc\\data\\Derm7pt\"\n",
    "train_loader, valid_loader, test_loader = preprocess_derm7pt(data_dir)\n",
    "\n",
    "# 返回 train, valid, test\n",
    "train_loader, valid_loader, test_loader"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
